<script setup>
import { computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useUserStore } from './stores/user'

const router = useRouter()
const route = useRoute()
const userStore = useUserStore()

// 菜单项数据
const menuItems = [
  { key: 'dashboard', label: '仪表盘', icon: '📊', path: '/dashboard' },
  { key: 'users', label: '用户管理', icon: '👥', path: '/users' },
  { key: 'products', label: '产品管理', icon: '📦', path: '/products' },
  { key: 'orders', label: '订单管理', icon: '🛒', path: '/orders' },
  { key: 'analytics', label: '数据分析', icon: '📈', path: '/analytics' },
  { key: 'settings', label: '系统设置', icon: '⚙️', path: '/settings' }
]

// 当前路由路径
const currentPath = computed(() => route.path)

// 获取当前页面标题
const currentPageTitle = computed(() => {
  const currentMenu = menuItems.find(item => item.path === currentPath.value)
  return currentMenu ? currentMenu.label : '管理后台'
})

// 导航到指定路由
const navigateTo = (path) => {
  router.push(path)
}

// 从store获取用户信息
const currentUser = computed(() => userStore.userInfo)
const isAuthenticated = computed(() => userStore.isAuthenticated)

// 退出登录
const logout = () => {
  userStore.logout()
  // 可以重定向到登录页面
  router.push('/login')
}
</script>

<template>
  <div class="admin-layout">
    <!-- 左侧导航栏 -->
    <aside class="sidebar">
      <div class="sidebar-header">
        <img alt="Vue logo" class="logo" src="./assets/logo.svg" width="40" height="40" />
        <h2 class="title">管理后台</h2>
      </div>
      
      <nav class="sidebar-nav">
        <ul class="nav-menu">
          <li 
            v-for="item in menuItems" 
            :key="item.key"
            class="nav-item"
            :class="{ active: currentPath === item.path }"
            @click="navigateTo(item.path)"
          >
            <span class="nav-icon">{{ item.icon }}</span>
            <span class="nav-label">{{ item.label }}</span>
          </li>
        </ul>
      </nav>
    </aside>

    <!-- 主要内容区域 -->
    <main class="main-content">
      <div class="content-header">
        <h1 class="page-title">{{ currentPageTitle }}</h1>
        <div class="user-info" v-if="isAuthenticated">
          <span class="username">{{ currentUser.name }}</span>
          <div class="avatar" @click="logout" :title="'点击退出登录'">
            {{ currentUser.avatar }}
          </div>
        </div>
      </div>

      <div class="content-body">
        <!-- 使用路由视图渲染页面内容 -->
        <router-view />
      </div>
    </main>
  </div>
</template>

<style scoped>
.admin-layout {
  display: flex;
  min-height: 100vh;
  background-color: #f5f5f5;
}

/* 左侧导航栏样式 */
.sidebar {
  width: 250px;
  background: linear-gradient(180deg, #2c3e50 0%, #34495e 100%);
  color: white;
  display: flex;
  flex-direction: column;
  box-shadow: 2px 0 8px rgba(0, 0, 0, 0.1);
}

.sidebar-header {
  padding: 20px;
  display: flex;
  align-items: center;
  gap: 12px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.logo {
  border-radius: 8px;
}

.title {
  font-size: 20px;
  font-weight: 600;
  margin: 0;
}

.sidebar-nav {
  flex: 1;
  padding: 20px 0;
}

.nav-menu {
  list-style: none;
  padding: 0;
  margin: 0;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 15px 20px;
  cursor: pointer;
  transition: all 0.3s ease;
  border-left: 3px solid transparent;
}

.nav-item:hover {
  background-color: rgba(255, 255, 255, 0.1);
}

.nav-item.active {
  background-color: rgba(255, 255, 255, 0.15);
  border-left-color: #3498db;
}

.nav-icon {
  font-size: 20px;
  width: 24px;
  text-align: center;
}

.nav-label {
  font-size: 16px;
  font-weight: 500;
}

/* 主要内容区域样式 */
.main-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.content-header {
  background: white;
  padding: 20px 30px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  z-index: 10;
}

.page-title {
  font-size: 24px;
  font-weight: 600;
  color: #2c3e50;
  margin: 0;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 12px;
}

.username {
  font-size: 16px;
  color: #7f8c8d;
}

.avatar {
  width: 40px;
  height: 40px;
  background: #3498db;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  color: white;
  cursor: pointer;
  transition: all 0.2s ease;
}

.avatar:hover {
  background: #2980b9;
  transform: scale(1.05);
}

.content-body {
  flex: 1;
  padding: 30px;
  overflow-y: auto;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .sidebar {
    width: 200px;
  }
  
  .sidebar-header {
    padding: 15px;
  }
  
  .title {
    font-size: 16px;
  }
  
  .content-header {
    padding: 15px 20px;
  }
  
  .page-title {
    font-size: 20px;
  }
  
  .content-body {
    padding: 20px;
  }
}
</style>
